/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cubelabs.nexus.dao;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import cubelabs.nexus.entity.Event;

/**
 *
 * @author chaitanya
 */
@Repository("eventDAO")
public class EventDAO extends BaseDAO<Event, Serializable>{

    public Boolean isConflicted(Long userId, Date startTime, Date endTime) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        crit.add(
        Restrictions.and(
            Restrictions.or(
                   Restrictions.and(
                       Restrictions.ge("start", startTime),
                       Restrictions.lt("end", startTime)),

                   Restrictions.and(
                       Restrictions.lt("start", endTime),
                       Restrictions.gt("end", endTime))
               ),
           Restrictions.eq("organizer.id", userId))
           );
        
        @SuppressWarnings("unchecked")
		List<Event> eventLst = (List<Event>) crit.list();
        if(eventLst != null && eventLst.size() > 0){
                return true;
        }
        return false;
    }
    
    /*
	 * findExpertTalks will return all expert talks which are yet to happen 
	 */
	public List<Event> findExpertTalks(){				
		List<Event>  eventLst= findByCriteria(
				 Restrictions.and( Restrictions.ge("start", new Date())));
		return eventLst;		
	}
	
	/*
	 * findExpertTalksByExpert will return all expert talks realted to a expert which are yet to happen 
	 */
	public List<Event> findExpertTalksByExpert(String name){				
		List<Event>  eventLst= findByCriteria(
				 Restrictions.and(Restrictions.eq("organizer.name", name) ,Restrictions.ge("start", new Date())));
		return eventLst;		
	}
}
